package com.seelyn.rhea.mvc.internal;

import com.seelyn.rhea.mvc.context.Request;
import com.seelyn.rhea.mvc.context.Response;
import com.seelyn.rhea.mvc.internal.matcher.RheaMatcher;
import com.seelyn.rhea.mvc.internal.matcher.UriMatcher;
import com.seelyn.rhea.mvc.render.View;
import com.seelyn.rhea.mvc.route.Route;
import com.seelyn.rhea.mvc.route.RouteMatching;

import java.util.Map;

/**
 * @author slacrey
 * @since 2016/1/24
 */
public abstract class InternalRoute implements RouteMatching {

    private final UriMatcher<String> matcher;

    protected InternalRoute(String path) {
        matcher = RheaMatcher.path(path);
    }

    @Override
    public boolean matching(String path) {
        return matcher.matching(path);
    }

    @Override
    public Map<String, String> getPathParams(String path) {
        return matcher.getPathParams(path);
    }

    public abstract String getPath();

    @Override
    public abstract <T> View<T> handle(Request request, Response response);

    public static InternalRoute create(final String path, final Route route) {
        return new RheaInternalRoute(path, route);
    }

    private static final class RheaInternalRoute extends InternalRoute {
        private final String path;
        private final Route route;

        private RheaInternalRoute(String path, Route route) {
            super(path);
            this.path = path;
            this.route = route;
        }

        @Override
        public String getPath() {
            return path;
        }

        @Override
        public <T> View<T> handle(Request request, Response response) {

            return route.handle(request, response);

        }
    }
}
